/**
 * @FileName: OrganizationTree.java
 * @Author
 * @Description:
 * @Date 2016年1月8日 上午11:05:22
 * @CopyRight ZTE Corporation
 */
package cn.hpclub.server.util;

import java.util.AbstractMap.SimpleEntry;
import java.util.ArrayList;
import java.util.List;
import java.util.Map.Entry;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.hpclub.server.bean.OrgNode;

public class OrganizationTree{
    private static Logger log = LoggerFactory.getLogger(OrganizationTree.class);

    /**
     * @Title: 获取机构树列表
     * @Description:根据当前帐号的机构ID，返回该机构和其子节点列表（不包含叶子节点）
     * @param orgId
     *            帐号所属机构ID
     * @return List<Entry<String,String>>
     */
    public static List<Entry<String, String>> getOrganizationList(int orgId){
        List<Entry<String, String>> orgList = new ArrayList<Entry<String, String>>();
        OrgNode root = getOrganizationTree(orgId);
        int level = 1;
        if(null != root){
            root.printNode("");
            getNodeList(root, orgList, level);
        }
        return orgList;
    }

    /**
     * @Title: 根据机构树，生成包含当前节点和子节点的机构列表
     * @param node
     *            结构节点
     * @param orgList
     *            结构列表
     * @param level
     *            当前节点所属层次 void
     */
    private static void getNodeList(OrgNode node, List<Entry<String, String>> orgList, int level){
        String prefixByLevel = getPrefixByLevel(level);
        orgList.add(new SimpleEntry<String, String>(prefixByLevel + node.getDisplayName(),
                String.valueOf(node.getId())));
        if(node.getSubNode() != null){
            for(OrgNode subNode : node.getSubNode()){
                getNodeList(subNode, orgList, (level + 1));
            }
        }
    }

    /**
     * @Title: getPrefixByLevel
     * @Description:根据节点层次返回机构名称前缀
     * @param level
     * @return String
     */
    private static String getPrefixByLevel(int level){
        String prefix = "";
        switch(level){
            case 2:
                prefix = "&nbsp;&nbsp;&nbsp;&nbsp;├&nbsp;";
                break;
            case 3:
                prefix = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;├&nbsp;";
                break;
            case 1:
            default:
                break;
        }
        return prefix;
    }

    /**
     * @Title: getOrganizationList
     * @Description:生成机构树（不包含叶子节点），该机构树的root节点为当前帐号所属机构
     * @param orgId
     *            当前帐号所属机构ID
     * @return List<OrgNode>
     */

    private static OrgNode getOrganizationTree(int orgId){
        OrgNode root = InterfaceUtil.getOrgTree(orgId, 2, false);
        if(root != null){
            root.printNode("");
            root = findOrgNode(root, orgId);
        }
        return root;
    }

    /**
     * @Title: findOrgNode
     * @Description:根据ID查找机构节点
     * @param node
     *            机构树节点
     * @param id
     *            结构ID void
     */
    private static OrgNode findOrgNode(OrgNode node, int id){
        if(node == null){
            return null;
        }
        if(node.getId() == id){
            return node;
        }
        if(node.getSubNode() == null){
            return null;
        }
        for(OrgNode subNode : node.getSubNode()){
            OrgNode parentNode = findOrgNode(subNode, id);
            if(parentNode != null){
                return parentNode;
            }
        }
        return null;
    }
}
